﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using Telerik.Web.UI;

namespace Web.SalesPipeline
{
    public partial class EditSalesPipeline : System.Web.UI.Page
    {
        static List<HtmlAnchor> aStepControls;
        static List<HtmlGenericControl> divStepControls;
        static USER user;
        static string requestEdit = string.Empty;
        static CONTACT_DETAIL conDe;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            this.Title = "Edit SalesPipline";
            user = (USER)Session["User"];
            if (user != null)
            {
                requestEdit = Request.QueryString["ContactID"];
                if (requestEdit != null && requestEdit.Trim() != "")
                {
                    aStepControls = new List<HtmlAnchor>();
                    aStepControls.Add(aStep1);
                    aStepControls.Add(aStep2);
                    aStepControls.Add(aStep3);
                    aStepControls.Add(aStep4);
                    aStepControls.Add(aStep5);
                    divStepControls = new List<HtmlGenericControl>();
                    divStepControls.Add(divStep1);
                    divStepControls.Add(divStep2);
                    divStepControls.Add(divStep3);
                    divStepControls.Add(divStep4);
                    divStepControls.Add(divStep5);
                    LoadContactInfo(Convert.ToInt64(requestEdit));
                    this.Title = conDe.CONTACT_NAME + ": " + "Edit SalesPipline";
                }
            }
        }
        private void showStep(string datastep)
        {
            foreach (HtmlAnchor a in aStepControls)
            {
                if (a.Attributes["data-step"] == datastep)
                    (a.Parent as HtmlGenericControl).Attributes["class"] = "active";
                else
                    (a.Parent as HtmlGenericControl).Attributes["class"] = "";
            }
            foreach (HtmlGenericControl div in divStepControls)
            {
                if (div.Attributes["data-step"] == datastep)
                    div.Style["display"] = "block";
                else
                    div.Style["display"] = "none";
            }
        }
        private void LoadContactInfo(long ConID)
        {
            conDe = ContactService.GetContactDetailByID(ConID);
            if (conDe != null)
            {
                txtNotes.Text = conDe.STATUS_NOTE;
                //Load Status Info
                //Appointment
                rbtnAppointmentYes.Checked = (conDe.APPOINTMENT != null && (bool)conDe.APPOINTMENT) ? true : false;
                rbtnAppointmentNo.Checked = rbtnAppointmentYes.Checked ? false : true;
                txtAppointmentNoReason.Text = conDe.NO_APPOINTMENT_REASON;
                divAppointmentNoReason.Style["display"] = rbtnAppointmentNo.Checked ? "block" : "none";
                btnStep1Next.Style["display"] = rbtnAppointmentYes.Checked ? "block" : "none";
                showStep("1");
                //Quotation issue
                if ((conDe.APPOINTMENT != null && (bool)conDe.APPOINTMENT))
                {
                    aStep2.Style["display"] = (conDe.APPOINTMENT != null && (bool)conDe.APPOINTMENT) ? "block" : "none";
                    rbtnQuotationIssueYes.Checked = (conDe.QUOTATION_ISSUE != null && (bool)conDe.QUOTATION_ISSUE) ? true : false;
                    rbtnQuotationIssueno.Checked = rbtnQuotationIssueYes.Checked ? false : true;
                    btnStep2Next.Style["display"] = rbtnQuotationIssueYes.Checked ? "block" : "none";
                    ddlQINoReason.Items[0].Selected = (conDe.QUOTATION_ISSUE_IS_PENDING != null && (bool)conDe.QUOTATION_ISSUE_IS_PENDING) ? true : false;
                    ddlQINoReason.Items[1].Selected = (conDe.QUOTATION_ISSUE_IS_REJECTED != null && (bool)conDe.QUOTATION_ISSUE_IS_REJECTED) ? true : false;
                    ddlQIPendingReason.Items[0].Selected = (conDe.QUOTATION_ISSUE_IS_CALL_BACK != null && (bool)conDe.QUOTATION_ISSUE_IS_CALL_BACK) ? true : false;
                    ddlQIPendingReason.Items[1].Selected = (conDe.QUOTATION_ISSUE_IS_APPOINTMENT != null && (bool)conDe.QUOTATION_ISSUE_IS_APPOINTMENT) ? true : false;
                    txtQIRejectedReason.Text = conDe.QUOTATION_ISSUE_REJECTED_RREASON;
                    divQINoReason.Style["display"] = !rbtnQuotationIssueYes.Checked ? "block" : "none";
                    divQIPendingReason.Style["display"] = (conDe.QUOTATION_ISSUE_IS_PENDING != null && (bool)conDe.QUOTATION_ISSUE_IS_PENDING) ? "block" : "none";
                    divQIRejectedReason.Style["display"] = (conDe.QUOTATION_ISSUE_IS_REJECTED != null && (bool)conDe.QUOTATION_ISSUE_IS_REJECTED) ? "block" : "none";
                    showStep("2");
                }
                //Deposited Collection
                if ((conDe.QUOTATION_ISSUE != null && (bool)conDe.QUOTATION_ISSUE) && conDe.DEPOSIT_COLLECTED != null)
                {
                    aStep3.Style["display"] = (conDe.QUOTATION_ISSUE != null && (bool)conDe.QUOTATION_ISSUE) ? "block" : "none";
                    rbtnDCYes.Checked = (conDe.DEPOSIT_COLLECTED != null && (bool)conDe.DEPOSIT_COLLECTED) ? true : false;
                    rbtnDCNo.Checked = rbtnDCYes.Checked ? false : true;
                    txtDCNoReason.Text = conDe.DEPOSIT_COLLECTED_NO_REASON;
                    rntxtAmount.Value = conDe.DEPOSIT_COLLECTED_AMOUNT;
                    rbtnDCDIIYes.Checked = (conDe.DEPOSIT_INVOICE_ISSUE != null && (bool)conDe.DEPOSIT_INVOICE_ISSUE) ? true : false;
                    if (conDe.DEPOSIT_INVOICE_ISSUE == null)
                    {
                        btnStep3Next.Style["display"] = "block";
                        rbtnDCDIIYes.Checked = true;
                    }
                    rbtnDCDIINo.Checked = rbtnDCDIIYes.Checked ? false : true;
                    txtDCDIINoReason.Text = conDe.NO_DEPOSIT_INVOICE_ISSUE_REASON;
                    divDCNoReason.Style["display"] = !rbtnDCYes.Checked ? "block" : "none";
                    divDCAmount.Style["display"] = rbtnDCYes.Checked ? "block" : "none";
                    divDCDII.Style["display"] = (rntxtAmount.Value != null && rntxtAmount.Value != 0) ? "block" : "none";
                    divDCDIINoReason.Style["display"] = (conDe.DEPOSIT_INVOICE_ISSUE != null && !(bool)conDe.DEPOSIT_INVOICE_ISSUE) ? "block" : "none";
                    btnStep3Next.Style["display"] = (conDe.DEPOSIT_INVOICE_ISSUE != null && (bool)conDe.DEPOSIT_INVOICE_ISSUE) ? "block" : "none";
                    showStep("3");
                }
                //Process order
                if ((conDe.DEPOSIT_INVOICE_ISSUE != null && (bool)conDe.DEPOSIT_INVOICE_ISSUE) && (conDe.PROCESS_ORDER_IS_PENDING != null || conDe.PROCESS_ORDER_IS_COMPLETED != null))
                {
                    aStep4.Style["display"] = (conDe.DEPOSIT_INVOICE_ISSUE != null && (bool)conDe.DEPOSIT_INVOICE_ISSUE) ? "block" : "none";
                    ddlPOStatus.Items[0].Selected = (conDe.PROCESS_ORDER_IS_PENDING != null && (bool)conDe.PROCESS_ORDER_IS_PENDING) ? true : false;
                    ddlPOStatus.Items[1].Selected = (conDe.PROCESS_ORDER_IS_COMPLETED != null && (bool)conDe.PROCESS_ORDER_IS_COMPLETED) ? true : false;
                    divPOPendingDate.Style["display"] = ddlPOStatus.Items[0].Selected ? "block" : "none";
                    dpPOPendingDate.SelectedDate = conDe.PROCESS_ORDER_PENDING_DATE;
                    divPOFullII.Style["display"] = ddlPOStatus.Items[1].Selected ? "block" : "none";
                    rbtnPOFullIIYes.Checked = conDe.PROCESS_ORDER_FULL_INVOICE_ISSUE == null ? true : (bool)conDe.PROCESS_ORDER_FULL_INVOICE_ISSUE;
                    rbtnPOFullIINo.Checked = rbtnPOFullIIYes.Checked ? false : true;
                    btnStep4Next.Style["display"] = ddlPOStatus.Items[1].Selected && rbtnPOFullIIYes.Checked ? "block" : "none";
                    showStep("4");
                }
                //Payment collection
                if ((conDe.PROCESS_ORDER_FULL_INVOICE_ISSUE != null && (bool)conDe.PROCESS_ORDER_FULL_INVOICE_ISSUE) && conDe.PAYMENT_COLLECTION != null)
                {
                    aStep5.Style["display"] = (conDe.PROCESS_ORDER_FULL_INVOICE_ISSUE != null && (bool)conDe.PROCESS_ORDER_FULL_INVOICE_ISSUE) ? "block" : "none";
                    rbtnPaidYes.Checked = (conDe.PAYMENT_COLLECTION != null && (bool)conDe.PAYMENT_COLLECTION) ? true : false;
                    rbtnPaidNo.Checked = rbtnPaidYes.Checked ? false : true;
                    divPaidNoReason.Style["display"] = rbtnPaidNo.Checked ? "block" : "none";
                    txtPaidNoReason.Text = conDe.PAYMENT_COLLECTION_NO_REASON;
                    btnStep5Done.Style["display"] = rbtnPaidYes.Checked ? "block" : "none";
                    showStep("5");
                }
                //Set step clicked value
                if (aStep2.Style["display"] == "block")
                    hdntxtNextStep1Clicked.Value = "1";
                if (aStep3.Style["display"] == "block")
                    hdntxtNextStep2Clicked.Value = "1";
                if (aStep4.Style["display"] == "block")
                    hdntxtNextStep3Clicked.Value = "1";
                if (aStep5.Style["display"] == "block")
                    hdntxtNextStep4Clicked.Value = "1";
            }
            else
            {
                divContent.Style["display"] = "none";
                divError.Style["display"] = "block";
            }
        }
        private PAYMENT_COLLECTIONS getPaymentCollection(bool poFullIIYes)
        {
            PAYMENT_COLLECTIONS paid = null;
            if (poFullIIYes && hdntxtNextStep4Clicked.Value == "1")
            {
                paid = new PAYMENT_COLLECTIONS();
                paid.PAYMENT_COLLECTION = rbtnPaidYes.Checked;
                paid.NO_REASON = (bool)paid.PAYMENT_COLLECTION ? null : txtPaidNoReason.Text.Trim();
                paid.IS_CLOSE_CASE = paid.PAYMENT_COLLECTION;
            }
            return paid;
        }
        private PROCESS_ORDERS getProcessOrder(bool dcDIIYes)
        {
            PROCESS_ORDERS po = null;
            if (dcDIIYes && hdntxtNextStep3Clicked.Value == "1")
            {
                po = new PROCESS_ORDERS();
                po.IS_PENDING = ddlPOStatus.SelectedValue == "0" ? true : false;
                po.PENDING_DATE = (bool)po.IS_PENDING ? dpPOPendingDate.SelectedDate : null;
                po.IS_COMPLETED = ddlPOStatus.SelectedValue == "1" ? true : false;
                if ((bool)po.IS_COMPLETED)
                    po.FULL_INVOICE_ISSUE = rbtnPOFullIIYes.Checked;
            }
            return po;
        }
        private DEPOSIT_COLLECTIONS getDepositCollection(bool qissYes)
        {
            DEPOSIT_COLLECTIONS dc = null;
            if (qissYes && hdntxtNextStep2Clicked.Value == "1")
            {
                dc = new DEPOSIT_COLLECTIONS();
                dc.DEPOSIT_COLLECTED = rbtnDCYes.Checked;
                dc.NO_REASON = (bool)dc.DEPOSIT_COLLECTED ? null : txtDCNoReason.Text.Trim();
                dc.AMOUNT = (bool)dc.DEPOSIT_COLLECTED ? rntxtAmount.Value : null;
                if ((bool)dc.DEPOSIT_COLLECTED && (dc.AMOUNT != null && dc.AMOUNT.Value != 0))
                {
                    dc.DEPOSIT_INVOICE_ISSUE = rbtnDCDIIYes.Checked;
                    dc.NO_DEPOSIT_INVOICE_ISSUE_REASON = (bool)dc.DEPOSIT_INVOICE_ISSUE ? null : txtDCDIINoReason.Text.Trim();
                }
            }
            return dc;
        }
        private QUOTATION_ISSUES getQuotationIssue(bool appointment)
        {
            QUOTATION_ISSUES qiss = null;
            if (appointment && hdntxtNextStep1Clicked.Value == "1")
            {
                qiss = new QUOTATION_ISSUES();
                qiss.QUOTATION_ISSUE = rbtnQuotationIssueYes.Checked;
                if (!(bool)qiss.QUOTATION_ISSUE)
                {
                    qiss.IS_PENDING = ddlQINoReason.SelectedValue == "0" ? true : false;
                    qiss.IS_REJECTED = ddlQINoReason.SelectedValue == "1" ? true : false;
                    if ((bool)qiss.IS_REJECTED)
                    {
                        qiss.REJECTED_REASON = txtQIRejectedReason.Text.Trim();
                    }
                    if ((bool)qiss.IS_PENDING)
                    {
                        qiss.IS_CALL_BACK = ddlQIPendingReason.SelectedValue == "0" ? true : false;
                        qiss.IS_APPOINTMENT = ddlQIPendingReason.SelectedValue == "1" ? true : false;
                    }
                }
            }
            return qiss;
        }
        private int UpdateSalesPipeline()
        {
            CONTACT contact = ContactService.GetContactByID(conDe.CONTACT_ID);
            contact.ID = Convert.ToInt64(requestEdit);
            DateTime dt = DateTime.Now;
            contact.DATE_ENTERED = conDe.DATE_ENTERED;
            contact.DATE_MODIFIED = dt;
            contact.STATUS_NOTE = txtNotes.Text;
            //Status info
            APPOINTMENT appoint = null;
            QUOTATION_ISSUES qiss = null;
            DEPOSIT_COLLECTIONS dc = null;
            PROCESS_ORDERS po = null;
            PAYMENT_COLLECTIONS paid = null;
            //if (ddlStatus.SelectedValue == "1")
            {
                appoint = new APPOINTMENT();
                appoint.APPOINTMENT1 = rbtnAppointmentYes.Checked;
                appoint.NO_REASON = rbtnAppointmentYes.Checked ? null : txtAppointmentNoReason.Text.Trim();
                contact.STATUS_FINAL_STEP = "1";
                qiss = getQuotationIssue((bool)appoint.APPOINTMENT1);
                if (qiss != null) contact.STATUS_FINAL_STEP = "2";
                dc = qiss != null ? getDepositCollection((bool)qiss.QUOTATION_ISSUE) : null;
                if (dc != null)
                {
                    contact.STATUS_FINAL_STEP = "31";
                    if (dc.DEPOSIT_INVOICE_ISSUE != null)
                        contact.STATUS_FINAL_STEP = "32";
                }
                po = dc != null ? getProcessOrder(dc.DEPOSIT_INVOICE_ISSUE == null ? false : (bool)dc.DEPOSIT_INVOICE_ISSUE) : null;
                if (po != null) contact.STATUS_FINAL_STEP = "4";
                paid = po != null ? getPaymentCollection(po.FULL_INVOICE_ISSUE == null ? false : (bool)po.FULL_INVOICE_ISSUE) : null;
                if (paid != null) contact.STATUS_FINAL_STEP = "5";
            }
            return ContactService.UpdateContact(contact, appoint, qiss, dc, po, paid);
        }
        protected void btnSave_ServerClick(object sender, EventArgs e)
        {
            int updateCon = UpdateSalesPipeline();
            if (updateCon == 1)
            {
                string script = "function f(){closeWin(); Sys.Application.remove_load(f);}Sys.Application.add_load(f);";
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", script, true); 
            }
            else
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('Can not update salespipeline !');", true);
        }
    }
}